package com.lanou3g.zjl.chating.home.friends;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.base.BaseActivity;
import com.lanou3g.zjl.chating.home.personal_center.CircleImageView;
import com.lanou3g.zjl.chating.utils.GetUserAvatarHelper;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by dllo on 15/11/23.
 */
public class InvitInfoListActivity extends BaseActivity {

    @Bind(R.id.invitList)
    ListView invitList;
    private ArrayList<String> data;

    @Override
    protected int getLayout() {
        return R.layout.activity_invitlist;
    }

    @Override
    protected void initData() {

        data = getIntent().getStringArrayListExtra("list");
        for (String s : data) {

            Log.i("lanou", s + "<><><><><><>");
        }
        //Log.i("lanou",data.get("username") + "<><><><><><>" + data.get("reason"));

        //绑定适配器
        invitList.setAdapter(new InvitListAdapter());

    }
    @OnClick(R.id.backToFriendsList1) void backToFriendsList1(){
        finish();
    }
    //适配器
    public class InvitListAdapter extends BaseAdapter {
//        private InvitYouToBeFriendHelper invitInfo;
//        private List<InvitYouToBeFriend> data;
//
//        //构造函数初始化数据
//        public InvitListAdapter() {
//
//            invitInfo = new InvitYouToBeFriendHelper(InvitInfoListActivity.this);
//            data = invitInfo.selecteData();
//        }

        @Override
        public int getCount() {

            return data != null && data.size() > 0 ? data.size() : 0;
        }

        @Override
        public String getItem(int i) {

            return data != null && data.size() > 0 ? data.get(i) : null;
        }

        @Override
        public long getItemId(int i) {

            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            ViewHolder holder = null;
            final String data = getItem(i);
            if (view == null) {
                view = LayoutInflater.from(InvitInfoListActivity.this).inflate(R.layout.invitlist_itemview, null);
                holder = new ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.invitName.setText(data);
            holder.invitReason.setText("加个好友吧!!");
            final ViewHolder finalHolder = holder;
            final ViewHolder finalHolder1 = holder;
            holder.idea.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //同意username的好友请求
                    try {
                        EMChatManager.getInstance().acceptInvitation(data);
                        Toast.makeText(InvitInfoListActivity.this, "你同意了" + data + "的好友请求", Toast.LENGTH_SHORT).show();
                        finalHolder.idea.setEnabled(false);
                        finalHolder1.refuseBtn.setEnabled(false);
                    } catch (EaseMobException e) {
                        e.printStackTrace();
                    }
                }
            });
            holder.refuseBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        EMChatManager.getInstance().refuseInvitation(data);
                        Toast.makeText(InvitInfoListActivity.this, "你拒绝了" + data + "的好友请求", Toast.LENGTH_SHORT).show();
                        finalHolder.idea.setEnabled(false);
                        finalHolder1.refuseBtn.setEnabled(false);
                    } catch (EaseMobException e) {
                        e.printStackTrace();
                    }
                }
            });
            GetUserAvatarHelper helper = new GetUserAvatarHelper(InvitInfoListActivity.this,holder.invitAvatar,data);
            helper.execute(data);
            return view;
        }

        class ViewHolder {

            @Bind(R.id.invitAvatar)
            CircleImageView invitAvatar;//邀请者的头像
            @Bind(R.id.invitName)
            TextView invitName;//邀请人的姓名
            @Bind(R.id.idea)
            TextView idea;//你的意见,同意或者拒绝
            @Bind(R.id.refuseBtn)
            TextView refuseBtn;//拒绝按钮
            @Bind(R.id.invitReason)
            TextView invitReason;//邀请者邀请你的原因
            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
